﻿//*********************************************************************
//
//  文件名：CollectInvoiceController
//
//  作者： Pwp
//
//  作用：收票管理模块
//
//  日期：2015-10-08
//
//*********************************************************************
using Framework.Common;
using Framework.Common.Exceptions;
using Framework.Common.Paged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouTravel.DomainModels;
using YouTravel.DomainService;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;
using YouTravel.QueryModels;
using YouTravel.ViewModels;

namespace Application.WebAPI.Controllers
{
	[RoutePrefix("api/collectinvoice")]
	public class CollectInvoiceController : BaseController
	{
		ICollectInvoiceService collectinvoiceService;
		DataResult dataResult = new DataResult();
		public CollectInvoiceController(ICollectInvoiceService collectinvoiceService)
            : base()
        {
			this.collectinvoiceService = collectinvoiceService;
        }
		/// <summary>
		/// 付款列表
		/// </summary>
		/// <param name="inputCollectInvoice"></param>
		/// <returns></returns>
		[HttpPost,Route("collectinvoicelist")]
		public DataResult GetCollectInvoiceList(PagedParam<CollectInvoiceQuery> inputCollectInvoice)
		{
			#region 角色验证
			//if (LoginUser.Type != 0 && LoginUser.Type != UserType.SpecialLine)
			//{
			//	throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			//}
			//如果当前用户为专线角色，只能查看专线下的付款列表
			if (LoginUser.Type == UserType.SpecialLine) {
				inputCollectInvoice.QueryDto.SpecialId = (int)LoginUser.SpecialId;
			}
			//如果当前用户为专线角色，只能查看专线下的付款列表
			if (LoginUser.Type == UserType.Branch)
			{
				inputCollectInvoice.QueryDto.BranchId = (int)LoginUser.BranchId;
			}
			#endregion

			#region 输入验证
			if (inputCollectInvoice.QueryDto.StartData.HasValue && inputCollectInvoice.QueryDto.EndData.HasValue) {
				if (inputCollectInvoice.QueryDto.StartData > inputCollectInvoice.QueryDto.EndData)
					throw new DataValidationException(string.Format(BusinessResourceMessage.ItemComparison, "提交开始时间", "提交结束时间"));
			}
			#endregion

			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = collectinvoiceService.GetList(inputCollectInvoice);
			return dataResult;
		}

		/// <summary>
		/// 提交发票（重新提交）
		/// </summary>
		/// <param name="inputContract"></param>
		/// <returns></returns>
		[HttpPost, Route("invoicesubmit")]
		public DataResult AddCollectInvoice(CollectInvoiceAddModel inputcontract)
		{
			#region 角色验证
			if (LoginUser.Type != 0 && LoginUser.Type != UserType.SpecialLine)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			decimal deMoney = 0;
			if (inputcontract.PaymentID == 0)
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "付款"));
			//if (inputcontract.SpecialId == 0)
			//	throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "专线"));
			//if (inputcontract.SupplyId == 0)
			//	throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "供应商"));
			//if (!CommonValidator.isMoney(inputcontract.Money.ToString()))
			//	throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "付款金额"));
			if (inputcontract.Group.Count<1)
				throw new DataValidationException(string.Format(BusinessResourceMessage.PleaseInput, "团信息"));
			foreach (CollectInvoiceAddModelGroup groupTemp in inputcontract.Group)
			{
				if (string.IsNullOrEmpty(groupTemp.Code))
					throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "团号"));
				if (groupTemp.Code.Length > 20)
					throw new DataValidationException(string.Format(BusinessResourceMessage.ItemLenError, "团号", "20"));
				if (string.IsNullOrEmpty(groupTemp.Name))
					throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "团名称"));
				if (groupTemp.Name.Length > 100)
					throw new DataValidationException(string.Format(BusinessResourceMessage.ItemLenError, "团名称", "100"));
				if (groupTemp.Info.Count < 1)
					throw new DataValidationException(string.Format(BusinessResourceMessage.PleaseInput, "发票"));
				foreach (CollectInvoiceAddModelInfo infoTemp in groupTemp.Info)
				{
					if (string.IsNullOrEmpty(infoTemp.Code))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "发票编号"));
					if (infoTemp.Code.Length > 50)
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemLenError, "发票编号", "50"));
					if (string.IsNullOrEmpty(infoTemp.Content))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "发票科目"));
					if (!CommonValidator.isMoney(infoTemp.Money.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "发票金额"));
					if (!CommonValidator.isDateTime(infoTemp.Date.ToString()))
						throw new DataValidationException(string.Format(BusinessResourceMessage.ItemFormatError, "发票日期"));
					deMoney += infoTemp.Money;
				}
			}

			#endregion
			inputcontract.Money = deMoney;
			inputcontract.OperatorID = LoginUser.ID;
			inputcontract.OperatorName = LoginUser.Name;
			inputcontract.OptionTime = DateTime.Now;
			collectinvoiceService.AddCollectInvoice(inputcontract);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}

		/// <summary>
		/// 付款发票详细
		/// </summary>
		/// <param name="inputPayment"></param>
		/// <returns></returns>
		[HttpPost, Route("collectinvoicedetail")]
		public DataResult DetailCollectInvoice(Payment inputPayment)
		{
			#region 角色验证
			//if (LoginUser.Type != 0 && LoginUser.Type != UserType.SpecialLine)
			//{
			//	throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			//}
			#endregion

			#region 输入验证
			if (inputPayment.ID == 0)
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "付款发票"));
			#endregion

			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			dataResult.Data = collectinvoiceService.DetailCollectInvoice(inputPayment.ID);
			return dataResult;
		}

		/// <summary>
		/// 确认付款发票
		/// </summary>
		/// <param name="inputPayment"></param>
		/// <returns></returns>
		[HttpPost, Route("examinecollectinvoice")]
		public DataResult ExamineCollectInvoice(CollectInvoiceStatusModel inputCollectInvoice)
		{
			#region 角色验证
			if (LoginUser.Type != 0 && LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (inputCollectInvoice.ID == 0)
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "付款发票"));
			#endregion

			inputCollectInvoice.OperatorID = LoginUser.ID;
			inputCollectInvoice.OperatorName = LoginUser.Name;
			inputCollectInvoice.OptionTime = DateTime.Now;
			inputCollectInvoice.Status = CollectInvoiceStatus.HasCollectInvoice;
			collectinvoiceService.EditCollectInvoiceStatus(inputCollectInvoice);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}

		/// <summary>
		/// 废弃付款发票
		/// </summary>
		/// <param name="inputPayment"></param>
		/// <returns></returns>
		[HttpPost, Route("abandonedcollectinvoice")]
		public DataResult AbandonedCollectInvoice(CollectInvoiceStatusModel inputCollectInvoice)
		{
			#region 角色验证
			if (LoginUser.Type != 0 && LoginUser.Type != UserType.Branch)
			{
				throw new DataOperationPermissions(BusinessResourceMessage.NoPower);
			}
			#endregion

			#region 输入验证
			if (inputCollectInvoice.ID == 0)
				throw new DataValidationException(string.Format(BusinessResourceMessage.Inexistent, "付款发票"));
			//if (string.IsNullOrEmpty(inputCollectInvoice.Remark))
			//	throw new DataValidationException(string.Format(BusinessResourceMessage.ItemCanNotNull, "废弃发票备注"));
			#endregion
			inputCollectInvoice.OperatorID = LoginUser.ID;
			inputCollectInvoice.OperatorName = LoginUser.Name;
			inputCollectInvoice.OptionTime = DateTime.Now;
			inputCollectInvoice.Status = CollectInvoiceStatus.Discard;
			collectinvoiceService.EditCollectInvoiceStatus(inputCollectInvoice);
			dataResult.Code = ResponseStatusCode.Success;
			dataResult.Msg = BusinessResourceMessage.Success;
			return dataResult;
		}
	}
}